package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class HomePage extends Utils {

    public static String url = "http://127.0.0.1:9580/?";
    public static String res = "";
    public HomePage() {
        super(url);
    }

    public void basicTest() throws InterruptedException, IOException {
        //检查列表模块切换
        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(2) > a > span.nav-link-title")).click();
        Thread.sleep(1000);
        res = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        assert res.equals("Java");

        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(3) > a > span.nav-link-title")).click();
        Thread.sleep(1000);
        res = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        assert res.equals("C++");

        driver.findElement(By.cssSelector("#topBoardList > li:nth-child(5) > a > span.nav-link-title")).click();
        Thread.sleep(1000);
        res = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        assert res.equals("MySQL");

        //检查搜索框输入
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.d-none.d-md-flex.me-3 > div > form > div > input")).sendKeys("111");
        String edit = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.d-none.d-md-flex.me-3 > div > form > div > input")).getAttribute("value");
        assert edit.equals("111");

        //黑白夜间切换
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-dark > svg > path:nth-child(2)")).click();
        res = driver.getCurrentUrl();
        assert res.equals("http://127.0.0.1:9580/?theme=dark");

        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > a.nav-link.px-0.hide-theme-light > svg")).click();
        res = driver.getCurrentUrl();
        assert res.equals("http://127.0.0.1:9580/?theme=light");

        //打开关闭站内私信
        driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg")).click();
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > h2"));

        driver.findElement(By.cssSelector("#index_message_offcanvasEnd > div.offcanvas-header > button")).click();
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post"));
    }

    //帖子详情页面
    public void enterDetail() throws IOException, InterruptedException {
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).click();
        //确保进入帖子详情页面
        driver.findElement(By.cssSelector("#details_article_content_title"));

        //检查帖子能否点赞
        driver.findElement(By.cssSelector("#details_btn_like_count")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert res.equals("成功");
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > span.close-jq-toast-single")).click();
        Thread.sleep(2000);

        //检查能否给帖子所属用户发私信

        //点击后取消发送
        driver.findElement(By.cssSelector("#btn_details_send_message")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        driver.findElement(By.cssSelector("#index_message_receive_cancel")).click();
        Thread.sleep(2000);

        //点击后直接关闭发送界面
        driver.findElement(By.cssSelector("#btn_details_send_message")).click();
        driver.findElement(By.cssSelector("#index_message_modal > div > div > div.modal-header > button")).click();
        getScreenShot(getClass().getName());

        //点击后不输入内容，直接点击发送
        driver.findElement(By.cssSelector("#btn_details_send_message")).click();
        driver.findElement(By.cssSelector("#btn_index_send_message")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        res = res.substring(res.lastIndexOf("\n") + 1).trim(); // 找到最后一个换行符，截取后面的内容
        getScreenShot(getClass().getName());
        assert res.equals("请输入要发送的内容");
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > span.close-jq-toast-single")).click();
        Thread.sleep(2000);

        //点击后正常发送
        driver.findElement(By.cssSelector("#btn_details_send_message")).click();
        driver.findElement(By.cssSelector("#index_message_receive_content")).sendKeys("用于测试，打扰抱歉");
        //确保文字正确输入
        res = driver.findElement(By.cssSelector("#index_message_receive_content")).getAttribute("value");
        getScreenShot(getClass().getName());
        assert res.equals("用于测试，打扰抱歉");
        driver.findElement(By.cssSelector("#btn_index_send_message")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(6));
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        getScreenShot(getClass().getName());
        res = res.substring(res.lastIndexOf("\n") + 1).trim(); // 找到最后一个换行符，截取后面的内容
        assert res.equals("发送成功");

        //帖子下进行留言
        //不输入内容直接回复
        WebElement target2 = driver.findElement(By.cssSelector("#details_btn_article_reply"));
        Thread.sleep(3000);
        Actions subAction2 = new Actions(driver);
        // 创建 Actions 对象
        subAction2.moveToElement(target2).click().perform();

        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        getScreenShot(getClass().getName());
        res = res.substring(res.lastIndexOf("\n") + 1).trim(); // 找到最后一个换行符，截取后面的内容
        assert res.equals("请输入回复内容");

        //正常回复
        WebElement ele = driver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"));
        Actions actions = new Actions(driver);
        //perform作用：为了在页面看到效果
        actions.moveToElement(ele).click(ele).perform();
        actions.sendKeys("键盘鼠标操作输入评论内容").perform();
        getScreenShot(getClass().getName());

        // 定位目标元素
        WebElement target = driver.findElement(By.cssSelector("#details_btn_article_reply"));
        Thread.sleep(3000);
        Actions subAction = new Actions(driver);
        // 创建 Actions 对象
        subAction.moveToElement(target).click().perform();

        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        getScreenShot(getClass().getName());
        res = res.substring(res.lastIndexOf("\n") + 1).trim(); // 找到最后一个换行符，截取后面的内容
        assert res.equals("回复成功");

        //能否返回论坛首页
        driver.navigate().back();
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong"));

    }

    //未登录状态下---访问首页
    //注意前面登陆用例下，登陆状态已经保存下来了
    //方法一：
    public void HomeNoLogin() throws InterruptedException, IOException {
        //保证未登录状态
////        如果有弹窗提示
//        Alert alert = driver.switchTo().alert();
//        alert.accept();

//        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
//        getScreenShot(getClass().getName());
        driver.findElement(By.cssSelector("#index_user_logout")).click();

        Thread.sleep(1000);
//        driver.get(url);    //页面逻辑bug，在退出登录后，重新get页面，依旧是登陆状态
        String expect = driver.getTitle();
        assert expect.equals("比特论坛 - 用户登录");

    }

}
